﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Text;
using SmartProject.Exceptions;

namespace SmartProject.BusinessRules
{
    public class UserNameSimple : BusinessRule
    {
        public override string Name
        {
            get { return "Simple username"; }
        }

        public override string Description
        {
            get 
            {
                return "Username cannot be less than 5 characters and it can consist of any characters.";
            }
        }

        public override void Implement(Entity data)
        {
            Data.DataParameter dataParameter = data.GetDataParameter("Username", false);
            string userName = (string)dataParameter.Value;
            if (userName.Length < 5)
            {
                throw new Exceptions.BusinessRuleViolation("The username cannot be less than 5 characters long.");
            }
            else
            {
                if (Membership.GetUser(userName) != null)
                {
                    throw new Exceptions.BusinessRuleViolation(String.Format("{0} is already in use.", userName));
                }
            }
        }

        public override bool QueryBooleanProperty(string propertyName)
        {
            switch (propertyName)
            {
                case "IsEnableUserNameTextBox":
                    return true;
            }

            throw new MissingPropertyName(String.Format(
                "Property name is missing: {0}", propertyName));
        }
    }
}
